import fitz
from operator import itemgetter
import os
import time


# 将pdf转换为图片
def pdf_to_image(pdfPath, imagePath):
    pdfDoc = fitz.open(pdfPath)
    for page_num in range(pdfDoc.page_count):
        page = pdfDoc[page_num]
        # 内容转换成图片
        mat = fitz.Matrix(1, 1).prerotate(0)
        max_height = 4200
        max_width = 3200
        pix = page.get_pixmap(matrix=mat, alpha=False)
        # print(pix.w, pix.h)
        # new_h, new_w = round(max_height / pix.h, 2), round(max_width / pix.w, 2)
        # print(new_h, new_w)
        # mat_min = min(new_h, new_w)
        # mat = fitz.Matrix(mat_min, mat_min).prerotate(0)
        pix = page.get_pixmap(matrix=mat, alpha=False)
        pix.pil_save(f'{imagePath}/img_{page_num + 1}.webp', optimize=True, dpi=(150, 150))
        #






def main():
    sTime = time.time()
    pdf_to_image('22政治新考案新高考全国b版教师用书.pdf', './img')

    # pdf_to_TextBlocks('1.pdf', 'F:\image\pdftxt.txt')
    eTime = time.time()
    s = eTime - sTime
    print('花费的时间为：%.2f秒' % (s))


if __name__ == '__main__':
    main()